<?php

if (!defined('BASEPATH'))
exit('No direct script access allowed');

class Profile extends MY_Controller {

	function __construct() {

		parent::__construct();
		
		$this->load->helper('url');
		
		if(!$this->checkUserLogin()){
        	redirect('auth/login');
        	return;
        }
        
        $this->load->library('form_validation');
        $this->load->library('parser');
        $this->load->helper(array('form', 'url'));
        $this->load->helper('date');
        $this->load->library('email');
        //$this->load->model('customer_model','customer');
		$this->load->model('country_model','country');
        $this->buildAccountBox();
    	$this->buildSupportBox();
        $this->buildNavigation();

	}

	public function _remap($method)
	{
		if ($method == 'change-password')
		{
			$this->changePassword();
		}
		else if ($method == 'change-email')
		{
			$this->changeEmail();
		}
		else if ($method == 'billing-information')
		{
			$this->billingInformation();
		}
		else if ($method == 'shipping-information')
		{
			$this->shippingInformation();
		}
		else if ($method == 'payment-information')
		{
			$this->paymentInformation();
		}
		else if ($method == 'review-order')
		{
			$this->orderReview();
		}
		else 
			$this->billingInformation();
	}

	function index() {

		$data['base_url'] = base_url();

		//$this->view();
		$this->billingInformation();
	}
	
	function email_check1($email) {
		$this->load->model('customer_model', 'customer', true);
		$result = $this->customer->getByEmail($email);
				
        //$result = $this->product->getByCode($code);
        if ($result != null) {
            $this->form_validation->set_message('email_check', '%s is not available.');
            return FALSE;
        } else {
            return TRUE;
        }
    }
    
	function email_check($email) {
		$this->load->model('customer_model', 'customer', true);
		$result = $this->customer->getByEmail($email);
				
        //$result = $this->product->getByCode($code);
        if ($result != null) {
        	$user = $this->session->userdata("customer");
		
			$userId = $user['id_user'];
			
			if ($userId != $result['id_user']){
		
            	$this->form_validation->set_message('email_check', 'Email ' . $email . ' is not available.');
            	return FALSE;
			}
			
			return TRUE;
        } 
        
        return TRUE;
    }
    
    
	function changeEmail(){
		$user = $this->session->userdata("customer");
		
		$userId = $user['id_user'];
		
		$this->load->model('country_model', 'country', true);
		$this->load->model('customer_model','customer');
		
		$data = $this->customer->getById($userId);
		//print_r($data);
		$this->load->model('register_user_profiles_model','profile_model');
		//$data = $this->profile_model->getByUserId($userId);
		$data['error_message'] = '';
		$data['message'] = '';
//		print_r($data);
		
		$this->load->helper('form');
		$post= $this->input->post();
		$user_data = array();
		$profile_id = null;
		
		if(!empty($post)){
			
			//$data = array_merge($data, $post);
			
			$post['error_message'] = '';	
			$this->load->library('form_validation');
			$this->form_validation->set_rules('password', 'lang:password', 'required|min_length[2]|max_length[50]');
			$this->form_validation->set_rules('active_email', 'lang:new_email', 'required|valid_email|max_length[100]|callback_email_check');  // callback_email_check|
			
			if ($this->form_validation->run() == FALSE) {
				$data['error_message'] = '<div id="error" style="color:red">' . validation_errors() . '</div>';
			}
			else{
				if($user['password'] != md5($post['password'])){
					$data['error_message'] = '<div id="error" style="color:red">' . lang('password_not_match') . '</div>';
				}
				else{
					$data['active_email'] = $post['active_email'];
					
					$this->load->helper('string'); 
					
					$data['active_key'] =  random_string('alnum', 32);
					
					$data['message'] = '<div id="success">' . sprintf( lang('message_change_email_success'), $post['active_email']) . '</div>';
					$result = $this->customer->update($data);
				}
				//$result = $this->profile_model->update($data);
			}
		}
		else{
			$data['error_message'] = '';
    		
		}
			
		$this->template->parse_view('right_content2column', 'default/front_end/profile/change_email_view',$data);
		$this->template->render();
	}
	
function changePassword(){
		$user = $this->session->userdata("customer");
		
		$userId = $user['id_user'];
		
		$this->load->model('country_model', 'country', true);
		$this->load->model('customer_model','customer');
		
		$data = $this->customer->getById($userId);
		//print_r($data);
		$this->load->model('register_user_profiles_model','profile_model');
		//$data = $this->profile_model->getByUserId($userId);
		$data['error_message'] = '';
		$data['message'] = '';
//		print_r($data);
		
		$this->load->helper('form');
		$post= $this->input->post();
		$user_data = array();
		$profile_id = null;
		
		if(!empty($post)){
			
			//$data = array_merge($data, $post);
			
			$post['error_message'] = '';	
			$this->load->library('form_validation');
			$this->form_validation->set_rules('password', 'lang:current_password', 'required|min_length[6]|max_length[50]');
			$this->form_validation->set_rules('new_password', 'lang:new_password', 'required|min_length[6]|max_length[50]');  // callback_email_check|
			$this->form_validation->set_rules('new_password2', 'lang:new_password_confirm', 'required|matches[new_password]|max_length[100]');  // callback_email_check|
			
			if ($this->form_validation->run() == FALSE) {
				$data['error_message'] = '<div id="error" style="color:red">' . validation_errors() . '</div>';
			}
			else{
				if($user['password'] != md5($post['password'])){
					$data['error_message'] = '<div id="error" style="color:red">' . lang('password_not_match') . '</div>';
				}
				else{
					$data['password'] = md5($post['new_password']);
					$this->load->helper('string'); 
					$data['message'] = lang('message_change_password_success');
					$result = $this->customer->update($data);
				}
				
			}
		}
		else{
			$data['error_message'] = '';
    		
		}
			
		$this->template->parse_view('right_content2column', 'default/front_end/profile/change_password_view',$data);
		$this->template->render();
	}

	function billingInformation(){
		$user = $this->session->userdata("customer");
		
		$userId = $user['id_user'];
		
		$this->load->model('register_user_profiles_model','profile_model');
		$data = $this->profile_model->getByUserId($userId);
		$data['error_message'] = '';
//		print_r($data);
		
		$this->load->model('country_model', 'country', true);
		$this->load->model('customer_model','customer');
		
		$this->load->helper('form');
		$post= $this->input->post();
		$user_data = array();
		$profile_id = null;
		
		if(!empty($post)){
			
			$data = array_merge($data, $post);
//			print_r($newarray);
//			return ;
			
			$post['error_message'] = '';	
			$this->load->library('form_validation');
			$this->form_validation->set_rules('b_firstname', 'lang:firstname', 'min_length[2]|max_length[50]');
			$this->form_validation->set_rules('b_lastname', 'lang:lastname', 'min_length[2]|max_length[50]');
			$this->form_validation->set_rules('b_company', 'lang:company', 'min_length[2]|max_length[50]');
			$this->form_validation->set_rules('b_phone', 'lang:phone', 'min_length[2]|max_length[50]');
			$this->form_validation->set_rules('b_fax', 'Fax', 'min_length[2]|max_length[50]');
			$this->form_validation->set_rules('b_address', 'lang:address', 'min_length[2]|max_length[150]');
			$this->form_validation->set_rules('b_city', 'lang:city', 'min_length[2]|max_length[50]');
			$this->form_validation->set_rules('b_zipcode', 'lang:zipcode', 'min_length[2]|max_length[50]');
			 
			//$this->form_validation->set_rules('txtEnName', 'lang:en_name', 'required|min_length[3]|max_length[50]');
			//$this->form_validation->set_rules('txtAbbreviation', 'lang:Abbreviation', 'required|min_length[2]|max_length[50]');
			 
			if ($this->form_validation->run() == FALSE) {
				$post['error_message'] = '<div id="error" style="color:red">' . validation_errors() . '</div>';
			}
			else{
				$result = $this->profile_model->update($data);
			}
		}
		else{
			$data['error_message'] = '';
    		
		}
		
		$member_id = $this->session->userdata('member_id');
		
		$countries = $this->country->getAll();
			$data_countries = array();
			for ($i = 0; $i < count($countries); $i++) {
				$data_countries[$countries[$i]['id']] = $countries[$i][$this->lang_code. '_name'];
			}
			$data['ddlCountries'] = form_dropdown('b_country', $data_countries, $data['b_country'], 'class="input-text"');
			
		$this->template->parse_view('right_content2column', 'default/front_end/profile/billing_information_view',$data);
		$this->template->render();
	}
	
function shippingInformation(){
	$user = $this->session->userdata("customer");
		
		$userId = $user['id_user'];
		
		$this->load->model('register_user_profiles_model','profile_model');
		$data = $this->profile_model->getByUserId($userId);
		$data['error_message'] = '';
		
		$user_data = array();
		$profile_id = null;

		$this->load->model('country_model', 'country', true);
		$this->load->model('customer_model','customer');
		
		$this->load->helper('form');
		$post= $this->input->post();
		$user_data = array();
		$profile_id = null;
		
		
		if(!empty($post)){
			//print_r($post);
			$data = array_merge($data, $post);	
			$this->load->library('form_validation');
			$this->form_validation->set_rules('s_firstname', 'lang:firstname', 'min_length[2]|max_length[50]');
			$this->form_validation->set_rules('s_lastname', 'lang:lastname', 'min_length[2]|max_length[50]');
			$this->form_validation->set_rules('s_company', 'lang:company', 'min_length[2]|max_length[50]');
			$this->form_validation->set_rules('s_phone', 'lang:phone', 'min_length[2]|max_length[50]');
			$this->form_validation->set_rules('s_fax', 'Fax', 'min_length[2]|max_length[50]');
			$this->form_validation->set_rules('s_address', 'lang:address', 'min_length[2]|max_length[150]');
			$this->form_validation->set_rules('s_city', 'lang:city', 'min_length[2]|max_length[50]');
			$this->form_validation->set_rules('s_zipcode', 'lang:zipcode', 'min_length[2]|max_length[50]');
			
			//$this->form_validation->set_rules('txtEnName', 'lang:en_name', 'required|min_length[3]|max_length[50]');
			//$this->form_validation->set_rules('txtAbbreviation', 'lang:Abbreviation', 'required|min_length[2]|max_length[50]');
			 
			if ($this->form_validation->run() == FALSE) {
				$post['error_message'] = '<div id="error" style="color:red">' . validation_errors() . '</div>';
			}
			else{
				$result = $this->profile_model->update($data);
			}
		}
		
		$member_id = $this->session->userdata('member_id');
		$countries = $this->country->getAll();
		$data_countries = array();
		for ($i = 0; $i < count($countries); $i++) {
			$data_countries[$countries[$i]['id']] = $countries[$i][$this->lang_code. '_name'];
		}
		$data['ddlCountries'] = form_dropdown('s_country', $data_countries, $data['s_country'], 'class="input-text"');
			
		$this->template->parse_view('right_content2column', 'default/front_end/profile/shipping_information_view',$data);
		$this->template->render();
	}
	
	function paymentInformation(){
		$this->load->model('country_model', 'country', true);
		$this->load->helper('form');
		$this->load->library('creditcard');
		
		$currentYear = date('Y', time());
		
		$post= $this->input->post();
		
		$error = false;
		
		if(!empty($post)){
			$post['error_message'] = '';	
			$this->load->library('form_validation');
			
			$payment = $this->input->post('payment_method');
			
			if(empty($payment)){
				//$this->form_validation->set_rules('payment_method', 'lang:payment_method', 'required');
				$post['payment_method'] = '';
				$post['error_message'] = '<p>' . lang('payment_method_is_required') . '</p>';
				$error = true;	
			}
			else if($post['payment_method'] != 'Paypal'){
				
				$card = $this->input->post('card_type');
				
				if (trim($card) == ''){
					$post['error_message'] .= '<p>' . lang('card_type_is_required') . '</p>';
		        	//$this->form_validation->set_message('_creditcard_number_check', lang('the_number_card_is_required'));
		        	$error = true;
				}
				
		        $number1 = $this->input->post('number1');
		        $number2 = $this->input->post('number2');
		        $number3 = $this->input->post('number3');
		        $number4 = $this->input->post('number4');
		        $number = $number1 . $number2 . $number3;
		        
		        $month = $this->input->post('expire_month');
		        $year = $this->input->post('expire_year');
		        
		        $this->load->library('creditcard');
		        
		        
		        if ($card != 'American Express') {
		        	$number .= $number4;
		        }
		        
		       
		        if(trim($number) == ''){
		        	$post['error_message'] .= '<p>' . lang('the_number_card_is_required') . '</p>';
		        	//$this->form_validation->set_message('_creditcard_number_check', lang('the_number_card_is_required'));
		        	$error = true;
		        }
		        else{
		        
			       $info1 = $this->creditcard->validateCreditcard_number($number);
			       //print_r($info1);
			       if($info1['status'] == 'false'){
			       		$post['error_message'] .= '<p>' . lang('credit_card_number_is_invalid') . '</p>';
						$error = true;
			       }
			       else{
			       		echo "$month, $year <br>";
			       		echo $info = $this->creditcard->validateCreditCardExpirationDate($month, $year);
        			
				       if($info === 'false'){
							$error = true;
							$post['error_message'] .= '<p>' . lang('the_card_is_expired') . '</p>';
				       }
			       }
			       
		        }
		       
		       
				//$this->form_validation->set_rules('card_type', 'lang:email', "required|valid_email|callback_creditcard_number_check[{$username},{$password}]");
				//$this->form_validation->set_rules('card_type', 'Credit card', "callback__creditcard_number_check");
				//$this->form_validation->set_rules('card_type', 'lang:email', "callback_creditcard_expire_check");
			}
			
			//$this->form_validation->set_rules('txtEnName', 'lang:en_name', 'required|min_length[3]|max_length[50]');
			//$this->form_validation->set_rules('txtAbbreviation', 'lang:Abbreviation', 'required|min_length[2]|max_length[50]');
			 
			if ($error == true /* || $this->form_validation->run() == FALSE*/) {
				$post['error_message'] = '<div id="error" style="color:red">' . $post['error_message'] /*. validation_errors() */. '</div>';
				
			}
			else{
				$this->session->set_userdata('payment_information', $post);
				/*if(isset($post['ship_to_this_address']))
				{
					redirect('checkout/payment-option');
				}
				else{
					redirect('checkout/shipping-information');
				}*/
				redirect('checkout/review-order');
			}
		}
		else{
			$post['error_message'] = '';
			$post['number1'] = '';
			$post['number2'] = '';
			$post['number3'] = '';
			$post['number4'] = '';
			$post['payment_method'] = '';
			$post['card_type'] = 'Select card type';
			$post['expire_month'] = 1;
			$post['expire_year'] = date('Y', time());
		}
		
		$member_id = $this->session->userdata('member_id');
		
		$card_types = array(
			'' => 'Select card type', 'Visa' => 'Visa card', 'MasterCard' => 'Master card', 'Discover' => 'Discovery card', 'American Express' => 'American Express card');
		
	
		
		$post['ddlCardType'] = form_dropdown('card_type', $card_types, $post['card_type'], 'class="input-text"');
		
		$expire_months = array('1' => '1' , '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9', '10' => '10', '11' => '11', '12' => '12');
		$post['ddlExpireMonth'] = form_dropdown('expire_month', $expire_months, $post['expire_month'], 'class="input-text"');
		
		$currentYear = date('Y', time());
		
		$expire_years = array();
		
		for ($i = $currentYear; $i <= $currentYear + 10; $i++){
			$expire_years[$i] = $i;
		}
		
		//$expire_years = array('2012' => '2012' , '2013' => '2013', '2014' => '2014', '2015' => '2015', '2016' => '2016', '2017' => '2017', '2018' => '2018', '2019' => '2019', '2020' => '2020',);
		$post['ddlExpireYear'] = form_dropdown('expire_year', $expire_years, $post['expire_year'], 'class="input-text"');
			
		$this->template->parse_view('right_content2column', 'default/front_end/checkout/payment_information_view',$post);
		$this->template->render();
	}

 function orderReview(){
    	$member_id = $this->session->userdata('member_id');
        
    	$data = array_merge($this->session->userdata['billing_information'], $this->session->userdata['shipping_information'], 
    				$this->session->userdata['payment_information']);
    	
    	$data['lb_name'] = lang('name');
    	$data['lb_picture'] = lang('picture');
    	$data['lb_quantity'] = lang('quantity');
    	$data['lb_price'] = lang('price');
    	$data['lb_total'] = lang('total');
    	$data['lb_total_amount'] = lang('total_amount');
    	$data['shopping_cart'] = lang('shopping_cart');
    	$data['home'] = lang('home');
        $data['cart'] = $this->cart->getCartMember( $member_id);
        $data['site_url'] = site_url();
       // if (empty($data['products']))
       // 	echo "Shopping cart is empty.";
        
        //print_r($data['products']);
        
        $lang = getLangCode();
        
        $totalAmount = 0;
        $index = 1;
        	foreach ($data['cart'] as &$item){
        		$item['name'] = $item[ $lang . '_name'];
        		$item['img'] = img(array('src' => getDirByDate($item['date_add'], PRODUCT_IMAGE_PATH) . '/' . $item['image'],  'height' => '60', 'width' => '60' ));
        		$item['delete'] = anchor('cart/delete/' . $item['product_id'], 'Delete', 'title="Delete '. $item['name'] .'"');
        		$item['subtotal'] = $item['price'] * $item['quantity'] ;
           		$item['product_input'] = form_hidden( 'product_id[]' , $item['product_id']);
           		$totalAmount += $item['subtotal'];
           		$item['index'] = $index;
           		$index++;
        	}
        $data['update_shopping_cart'] = anchor('#', lang('update_shopping_cart'), ' class="buttonlink" id="update_cart"');
        $data['process_to_checkout'] = anchor('checkout', lang('process_to_checkout'), ' class="buttonlink"');
        $data['continue_shopping'] = anchor('product', lang('continue_shopping'), ' class="buttonlink" title="Continue Shopping"');
        
        $data['action'] = site_url('cart/update');
        $data['total_item'] = $totalAmount;
        
        if ($data['shipping_method'] == 'usps_Priority Mail International')
        	$data['shipping_fee'] = 46.2;
        elseif ($data['shipping_method'] == 'usps_Express Mail International')
        	$data['shipping_fee'] = 58.5;	
        
        $data['total'] = $data['shipping_fee'] + $data['total_item'] ;	
        
        
        
        
        
        
        
        
		$data['your_order_code'] = lang('');
		$data['we_will_call_you_winthin_24_hour_to_comfirm_the_order'] = lang('we_will_call_you_winthin_24_hour_to_comfirm_the_order');
		$data['ordered_date'] = lang('ordered_date');
		$data['date'] = 'ngay dat hang o day';
		$data['order_value_total'] = lang('order_value_total');
		$data['order_code'] = lang('order_code');
		$data['lb_shipping_fee'] = lang('shipping_fee'); 
		//$data['shipping_fee'] = 988.23;
		//$data['total'] = 454556;
		$data['lb_total'] = lang('total');
		$data['total_value_of_payment'] = lang('total_value_of_payment');
		$data['order_information'] = lang('order_information');
		$data['order_detail'] = lang('order_detail');
		$data['shipping_information'] = lang('shipping_informations');
		//$data['shipping_method'] = 'UPS';
		$data['lb_shipping_method'] = lang('shipping_method');
		$data['receiver'] = lang('receiver');
		$data['address'] = lang('address');
		$data['s_country_name'] = getCountry($data['s_country']);
		$data['b_country_name'] = getCountry($data['b_country']);
		$data[''] = lang('');
		$data[''] = lang('');
		$data[''] = lang('');
		$data[''] = lang('');
		$data[''] = lang('');
		
		$data['order_id'] = 983434;
		//echo $this->load->view('default/front_end/order/view_email_order', $data, true);	
		echo $this->parser->parse('default/front_end/order/view_email_order', $data, true);
        
        
        //$this->template->parse_view('top_content1', 'default/front_end/checkout/order_review_view',$data);
        //$this->template->render();  
    }
    
function _creditcard_number_check($field) {
	
	$this->form_validation->set_message('_creditcard_number_check', lang('the_number_card_is_required'));
	return false;
        	
      
        echo $card = $this->input->post('card_type');
        $number1 = $this->input->post('number1');
        $number2 = $this->input->post('number2');
        $number3 = $this->input->post('number3');
        $number4 = $this->input->post('number4');
        echo "Goi : creditcard_number_check";
        $number = $number1 . $number2 . $number3;
        
        $this->load->library('creditcard');
        
        
        if ($card == 'American Express') {
        	$number .= $number4;
        }
        
        if(trim($number) == ''){
        	$this->form_validation->set_message('_creditcard_number_check', lang('the_number_card_is_required'));
        	return false;
        }
        	
       $info = $this->creditcard->validateCreditcard_number($number);
        
       if($info['status'])
			return true;
       else{
            $this->form_validation->set_message('creditcard_number_check', lang('credit_card_number_is_invalid'));
            return  false;
       } 
        
       return true;
    }

function creditcard_expire_check($field = '', $params = '') {
        $card = $this->input->post('card_type');
        $month = $this->input->post('expire_month');
        $year = $this->input->post('expire_year');
        
        $this->load->library('creditcard');
        
        $result = TRUE;
        
        $info = $this->creditcard->validateCreditCardExpirationDate($month, $year);
        
       if($info['status'])
			 $result = TRUE;
       else{
            $this->form_validation->set_message('creditcard_expire_check', lang('the_card_is_expired'));
             $result = FALSE;
        } 
        
        return $result;
    }
    
function friendly_url_check($url) {
        $result = $this->pcategory->getByLinkRewrite($url);
        if ($result != null) {
            $this->form_validation->set_message('friendly_url_check', '%s is not available.');
            return FALSE;
        } else {
            return TRUE;
        }
    }
    
	function view(){
		$member_id = $this->session->userdata('member_id');

		$data['lb_name'] = lang('name');
		$data['lb_picture'] = lang('picture');
		$data['lb_quantity'] = lang('quantity');
		$data['lb_price'] = lang('price');
		$data['lb_total'] = lang('total');
		$data['lb_total_amount'] = lang('total_amount');
		$data['shopping_cart'] = lang('shopping_cart');
		$data['home'] = lang('home');
		$data['cart'] = $this->cart->getCartMember( $member_id);

		// if (empty($data['products']))
		// 	echo "Shopping cart is empty.";

		//print_r($data['products']);

		$lang = getLangCode();

		$totalAmount = 0;

		foreach ($data['cart'] as &$item){
			$item['name'] = $item[ $lang . '_name'];
			$item['img'] = img(array('src' => getDirByDate($item['date_add'], PRODUCT_IMAGE_PATH) . '/' . $item['image'],  'height' => '60', 'width' => '60' ));
			$item['delete'] = anchor('cart/delete/' . $item['product_id'], 'Delete', 'title="Delete '. $item['name'] .'"');
			$item['total'] = $item['price'] * $item['quantity'] ;
			$item['quantity'] = form_input(array(
             					 'name'        => 'quantity[]',
			//'id'          => 'price',
              					'value'       => $item['quantity'],
              					'maxlength'   => '100',
              					'size'        => '50',
              					'style'       => 'width:50px',
			));
			$item['product_input'] = form_hidden( 'product_id[]' , $item['product_id']);
			$totalAmount += $item['total'];
		}
		$data['update_shopping_cart'] = anchor('#', lang('update_shopping_cart'), ' class="buttonlink" id="update_cart"');
		$data['process_to_checkout'] = anchor('checkout', lang('process_to_checkout'), ' class="buttonlink"');
		$data['continue_shopping'] = anchor('product', lang('continue_shopping'), ' class="buttonlink" title="Continue Shopping"');

		$data['action'] = site_url('cart/update');
		$data['total_amount'] = $totalAmount;

		$this->template->parse_view('top_content1', 'default/front_end/cart/cart_view',$data);
		$this->template->render();
	}

	function buildCheckoutBox(){
		 
		$data['lb_'] = '';
		$this->template->parse_view('left_content1', 'default/front_end/checkout/checkout_box',$data);
	}

	function add($product_id) {

		$member_id = $this->session->userdata('member_id');
			
		if (empty($member_id)){
			redirect('login');
			return;
		}

		$product = $this->product->getById($product_id);
			
		if (!empty($product)){
			$item = $this->cart->getCartMember($member_id, $product_id);
			if (empty($item))
			$result = $this->cart->insert(array('member_id' => $member_id, 'product_id' => $product_id, 'quantity' => 1));
			else
			$result = $this->cart->update(array('member_id' => $member_id, 'product_id' => $product_id, 'quantity' => 1 + $item[0]['quantity']));
		}
			
		$this->view();
	}

	function update() {

		$member_id = $this->session->userdata('member_id');
			
		if (empty($member_id)){
			redirect('login');
			return;
		}
			
		$product_ids = $this->input->post('product_id');
		$product_quantities = $this->input->post('quantity');
			
		//print_r($product_ids);
		//print_r($_POST);
		//echo "Number item: " . count($product_ids);
		//return ;
			
		for ($i = 0 ; $i < count($product_ids); $i++){
			$product_id = $product_ids[$i];
			$quantity = intval($product_quantities[$i]);
			if ($quantity == 0){
				$this->cart->delete($member_id, $product_id);
			}
			else{
				$this->cart->update(array('member_id' => $member_id, 'product_id' => $product_id, 'quantity' => $quantity));
			}
		}
			
		$this->view();
	}

}

?>